var GraceJS = require("../../Grace.JS/grace.js");
import {
    apiServer
} from '../../common.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        limit: 10,
        list: [],
        tags: '',
        isEmpty: false
    },
    tags: function(e){
        var url = e.currentTarget.dataset.url;
        GraceJS.navigate(url);
    },
    // 查看详细
    detail: function (e) {
        var url = e.currentTarget.dataset.url;
        GraceJS.navigate(url);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        var that = this;
        var titles = "#"+options.tags+"#"
        GraceJS.setNavBar({
            title: titles
        });
        that.setData({
            tags: options.tags
        }, function () {
            that.getList()
        })
    },

    // 获取列表
    getList: function () {
        var that = this;
        wx.showLoading({
            title: 'Loading...',
        })
        that.setData({ loadingType: 1 })
        GraceJS.post(
            apiServer + 'resume.boke/tags', {
            page: that.data.page,
            limit: that.data.limit,
            tags: that.data.tags,
        }, 'json', {},
            function (res) {
                if (res.code == 0) {
                    var nowList = GraceJS.arrayConcat(that.data.list, res.data);
                    that.setData({
                        list: nowList,
                        isEmpty: false,
                        page: that.data.page + 1,
                        loadingType: res.data.length < that.data.limit ? 3 : 1,
                    }, function () {
                        if (that.data.list.length == 0) {
                            that.setData({
                                isEmpty: true
                            })
                        }
                        wx.hideLoading();
                    })
                } else {
                    GraceJS.msg(res.message)
                    wx.hideLoading();

                }
            }
        )
    },

    //下拉刷新事件
    onPullDownRefresh: function () {
        this.setData({
            list: [],
            page: 1,
            limit: 10,
            isEmpty: false,
        });
        this.getList();
        // 结束下拉刷新
        wx.stopPullDownRefresh();
        GraceJS.msg('刷新成功');
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        //避免多次触发
        if (that.data.loadingType == 3) {
            return;
        }
        that.getList();
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})